/**
 * 事件记录表管理初始化
 */
var EventRecord = {
    id: "EventRecordTable",
    seItem: null,
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
EventRecord.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'ID', field: 'id', visible: true, align: 'center', valign: 'middle'},
        {title: '时间', field: 'timeSecond', visible: true, align: 'center', valign: 'middle'},
        {title: '设备名称', field: 'equipmentName', visible: true, align: 'center', valign: 'middle'},
        {title: '门名称', field: 'doorName', visible: true, align: 'center', valign: 'middle'},
        {title: '事件名称', field: 'eventName', visible: true, align: 'center', valign: 'middle'},
        {title: '人员编号', field: 'pin', visible: true, align: 'center', valign: 'middle'},
        {title: '姓名', field: 'userName', visible: true, align: 'center', valign: 'middle'},
        {title: '卡号', field: 'cardNo', visible: true, align: 'center', valign: 'middle'},
        {title: '部门名称', field: 'deptName', visible: true, align: 'center', valign: 'middle'},
        {title: '验证方式', field: 'verifiedName', visible: true, align: 'center', valign: 'middle'},
        {title: '出入状态', field: 'inoutState', visible: true, align: 'center', valign: 'middle'},
        {title: '区域名称', field: 'areaName', visible: true, align: 'center', valign: 'middle'},
        {title: '是否正常', field: 'eventStatus',
            cellStyle: function (value, row, index) {
                if (value == 0) {
                    return {css:{"background-color":"red"}};
                }else if(value == 1) {
                    return {css:{"background-color":"green"}};
                }
            },
            formatter: function (value, row, index) {
                return '';
            },
            visible: true, align: 'center', valign: 'middle'},
    ];
};

/**
 * 检查是否选中
 */
EventRecord.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Feng.info("请先选中表格中的某一记录！");
        return false;
    }else{
        EventRecord.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加事件记录表
 */
EventRecord.openAddEventRecord = function () {
    var index = layer.open({
        type: 2,
        title: '添加事件记录表',
        area: ['85%', '85%'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/door/event/record/add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看事件记录表详情
 */
EventRecord.openEventRecordDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '系统参数详情',
            area: ['85%', '85%'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/door/event/record/edit/' + EventRecord.seItem.id
        });
        this.layerIndex = index;
    }
};

/**
 * 删除事件记录表
 */
EventRecord.delete = function () {
    if (this.check()) {
        var ajax = new $ax(Feng.ctxPath + "/door/event/record/delete", function (data) {
            Feng.success("删除成功!");
            EventRecord.table.refresh();
        }, function (data) {
            Feng.error("删除失败!" + data.responseJSON.message + "!");
        });
        ajax.set("id",this.seItem.id);
        ajax.start();
    }
};

/**
 * 查询事件记录表列表
 */
EventRecord.search = function () {
    var queryData = {};
    queryData['userId'] = $("#userId").val();
    queryData['equipmentName'] = $("#equipmentName").val();
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();
    EventRecord.table.refresh({query: queryData});
};
/**
 * 重置查询条件
 */
EventRecord.reset = function () {
    $('#userId').val('');
    $('#equipmentName').val('');
    $('#beginTime').val('');
    $('#endTime').val('');
    this.search();
};

$(function () {
    var defaultColunms = EventRecord.initColumn();
    var table = new BSTable(EventRecord.id, "/door/event/record/listEventRecordDto", defaultColunms);
    table.setPaginationType("server");
    EventRecord.table = table.init();
});
